import time
import unittest

from common.base import Base, get_driver
from selenium.webdriver.common.by import By

class RegisterPage(Base):
    register_url = 'http://localhost:8080/ecshop/user.php?act=register'

    # 定位用户名输入框
    def input_username(self, content):
        self.send_keys((By.ID, 'username'), content)



    # 定位email输入框
    def input_email(self, content):
        self.send_keys((By.ID, 'email'), content)



    # 定位密码输入框
    def input_password(self, content):
        self.send_keys((By.ID, 'password1'), content)



    # 定位确认密码输入框
    def input_conform_password(self, content):
        self.send_keys((By.ID, 'conform_password'), content)



    # 定位手机号输入框
    def input_phone(self, content):
        self.send_keys((By.NAME, 'extend_field5'), content)

    # 定位接受协议的单选框
    def input_radio(self):
        self.click((By.NAME, 'I'))

    # 定位《立即注册》按钮
    def input_submit(self):
        self.click((By.NAME, 'Submit'))

    # 定位用户名输入框右侧提示信息
    def username_notice(self):
        # self.get_attribute((By.ID, 'username_notice'), 'outerHTML')
        # return self.get_text((By.ID, 'username_notice'))
        return self.get_attribute((By.ID, 'username_notice'), 'innerHTML')
        # print(self.get_attribute((By.XPATH, '//*[@id="username_notice"]'), 'innerHTML'))

    # 定位email输入框右侧提示信息
    def email_notice(self):
        return self.get_attribute((By.ID, 'email_notice'), 'innerHTML')

    # 定位密码输入框右侧提示信息
    def password_notice(self):
        return self.get_attribute((By.XPATH, '//*[@id="password_notice"]'), 'innerHTML')

    # 定位确认密码输入框右侧提示信息
    def conform_password_notice(self):
        return self.get_attribute((By.ID, 'conform_password_notice'), 'innerHTML')

if __name__ == '__main__':
    # 创建driver
    driver = get_driver()
    driver.get('http://localhost:8080/ecshop/user.php?act=register')
    # 创建页面对象
    register = RegisterPage(driver)
    time.sleep(1)



    register.input_username('test9')
    time.sleep(1)

    register.input_email('999@163.com')
    time.sleep(1)
    register.input_password('123456')
    time.sleep(1)
    register.input_conform_password('123456')
    time.sleep(1)

    register.input_phone('17716478711')
    time.sleep(1)
    # register.input_radio()
    # time.sleep(1)
    print(register.username_notice())
    print(register.email_notice())


    class RegisterTestCase(unittest.TestCase):
        def test01(self):
            self.register.input_username('eee')
            # 点击右上角的关键字输入框，使用户名输入框失去焦点
            self.register.click((By.NAME, 'extend_field2'))
            time.sleep(2)
            # TODO 断言，在用户名输入框右边出现《* 可以注册》信息，视为注册用户名符合规定
            # 预期结果 与 实际结果的对比
            # 定位实际结果
            actual_username_notice = self.register.username_notice()
            self.assertEqual('* 可以注册', actual_username_notice, msg='用户名不符合规定')

    #
    # # register.input_submit()
    # register.username_notice()



    register.quit(3)